package com.atguigu.yygh.hosp.controller.Api;

import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.common.helper.HttpRequestHelper;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.result.ResultCodeEnum;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.hosp.service.ScheduleService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.model.hosp.Schedule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/api/hosp")
public class ApiController {
    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private HospitalSetService hospitalSetService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private ScheduleService scheduleService;

    //保存医院
    @PostMapping("saveHospital")
    public Result saveHosp(HttpServletRequest request){
        //因为医院上传系统会发送来一个map，这里要接收一个map集合
        Map<String, String[]> parameterMap = request.getParameterMap();
        //可以调用switchMap将Map<String,String[]>转换成Map<String,Object>
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);

        //医院接口签名校验
        if(!hospitalSetService.check(map)){
            //签名错误异常，抛出异常
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        //由于图片做了base处理，base把+号字符串都变为了" "空格，所以需要改回加号+
        String logoData = (String)map.get("logoData");
        if (!StringUtils.isEmpty(logoData)){
            logoData = logoData.replaceAll(" ", "+");
            map.put("logoData",logoData);
        }

        //调用接口保存医院信息
        hospitalService.save(map);
        return Result.ok();
    }


    //查询医院
    @PostMapping("hospital/show")
    public Result getHospital(HttpServletRequest request){
        //因为医院上传系统会发送来一个map，这里要接收一个map集合
        Map<String, String[]> parameterMap = request.getParameterMap();
        //可以调用switchMap将Map<String,String[]>转换成Map<String,Object>
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        //医院接口签名校验
        if(!hospitalSetService.check(map)){
            //签名错误异常，抛出异常
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        Hospital hospital = hospitalService.getByHoscode((String)map.get("hoscode"));
        return Result.ok(hospital);
    }

    //上传科室信息
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request){
        //因为医院上传系统会发送来一个map，这里要接收一个map集合
        Map<String, String[]> parameterMap = request.getParameterMap();
        //可以调用switchMap将Map<String,String[]>转换成Map<String,Object>
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        //医院接口签名校验
        if(!hospitalSetService.check(map)){
            //签名错误异常，抛出异常
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        //保存科室信息
        departmentService.save(map);
        return Result.ok();
    }

    //查询科室接口
    @PostMapping("department/list")
    public Result getDepartment(HttpServletRequest request){
        //因为医院上传系统会发送来一个map，这里要接收一个map集合
        Map<String, String[]> parameterMap = request.getParameterMap();
        //可以调用switchMap将Map<String,String[]>转换成Map<String,Object>
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        //医院接口签名校验
        if(!hospitalSetService.check(map)){
            //签名错误异常，抛出异常
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //查询科室信息
        Page<Department> dPage = departmentService.getDepartmentPage(map);
        return Result.ok(dPage);
    }

    //删除科室接口
    @PostMapping("department/remove")
    public Result deleteDepartment(HttpServletRequest request){
        //因为医院上传系统会发送来一个map，这里要接收一个map集合
        Map<String, String[]> parameterMap = request.getParameterMap();
        //可以调用switchMap将Map<String,String[]>转换成Map<String,Object>
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        //医院接口签名校验
        if(!hospitalSetService.check(map)){
            //签名错误异常，抛出异常
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //查询科室信息
        departmentService.deleteDepartment(map);
        return Result.ok();
    }

    //添加排班
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {
        //因为医院上传系统会发送来一个map，这里要接收一个map集合
        Map<String, String[]> parameterMap = request.getParameterMap();
        //可以调用switchMap将Map<String,String[]>转换成Map<String,Object>
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        //医院接口签名校验
        if(!hospitalSetService.check(map)){
            //签名错误异常，抛出异常
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //调用添加服务方法
        scheduleService.save(map);

        return Result.ok();
    }

    //查询排班
    @PostMapping("schedule/list")
    public Result getSchedule(HttpServletRequest request) {
        //因为医院上传系统会发送来一个map，这里要接收一个map集合
        Map<String, String[]> parameterMap = request.getParameterMap();
        //可以调用switchMap将Map<String,String[]>转换成Map<String,Object>
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        //医院接口签名校验
        if(!hospitalSetService.check(map)){
            //签名错误异常，抛出异常
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //调用页面分页服务方法
        Page<Schedule> page = scheduleService.getSchedulePage(map);
        return Result.ok(page);
    }

    //删除排班
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request) {
        //因为医院上传系统会发送来一个map，这里要接收一个map集合
        Map<String, String[]> parameterMap = request.getParameterMap();
        //可以调用switchMap将Map<String,String[]>转换成Map<String,Object>
        Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
        //医院接口签名校验
        if(!hospitalSetService.check(map)){
            //签名错误异常，抛出异常
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //调用删除服务方法
        scheduleService.removeSchedule(map);
        return Result.ok();
    }
}
